// pages/household/household.js
const app = getApp()
const wh = require("../../utils/wh.js");
import WxValidate from "../../utils/WxValidate.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    auth_modal: false, // 授权窗口
    canIUse: wx.canIUse('button.open-type.getUserInfo'), 
    username: '这里是昵称', // 用户信息
    avatar: '../../images/head.png',
    device: [{
      dc_name: '选择卫浴',
      dc_id: ''
    }],
    array: [{
      dc_name: '选择品牌',
      dc_id: ''
    } ],
    array1: [{
      dc_name: '选择品牌',
      dc_id: ''
    } ],
    rules: [{
      dc_name: '选择规格',
      dc_id: ''
    }],
    rules1: [{
      dc_name: '选择规格',
      dc_id: ''
    }],
    index: 0,
    index0: 0,
    index1: 0,
    date: '计划安装时间',
    type:5,
    type1:4,
    dc_id: ''
  },
  // 选择卫浴
  bindDeviceChange: function(e) {
    let array=this.data.array1;
    let rules = this.data.rules1;
    this.setData({
      index1: e.detail.value, 
      array:array,
      rules: rules,
      index:0,
      index0:0
    })
    this.secondbt()
  },
  // 选择品牌
  bindPickerChange: function(e) {
    let rules = this.data.rules1;
    this.setData({
      index: e.detail.value,
      rules: rules,
      index0: 0
    })
    this.threebt()
  },
  // 选择规格
  bindRuleChange: function(e) {
    this.setData({
      index0: e.detail.value
    })
  },
  // 计划购买时间
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
  // 表单提交
  bindsubmit: function(e) {
    let is_user_save = app.is_user_save_data;
    if (is_user_save == -1) {
      wx.showToast({
        title: '请先完善用户信息',
        icon: 'success',
        duration: 2000,
        complete: function () {
          setTimeout(function () {
            wx.navigateTo({
              url: '../personData/personData'
            })
          }, 2000);
        }
      });
      return;
    }
    let rules = {
      date: {
        required: true,
        dateISO: true
      },
    }
    // 验证字段的提示信息，若不传则调用默认的信息  
    let messages = {
        date: {
          required: '请选择计划购买时间',
          dateISO: '请选择计划购买时间',
        },
      },
      Validate = new WxValidate(rules, messages);
    var that = this;
    var formData = e.detail.value;
    if (!Validate.checkForm(e)) {
      const error = Validate.errorList[0]
      //提示信息  
      wx.showToast({
        title: error.msg,
        icon: 'none'
      });
      return false
    } else if (e.detail.value.household_app == "选择家电设备") {
      wx.showToast({
        title: "请选择家电设备",
        icon: 'none'
      });
    } else if (e.detail.value.brand == "选择品牌") {
      wx.showToast({
        title: "请选择品牌",
        icon: 'none'
      });
    } else if (e.detail.value.standard == "选择规格") {
      wx.showToast({
        title: "请选择规格",
        icon: 'none'
      });
    } else {
      let postUrl = '/api/madetoorder.decorate.index/add_decorate_apply',
        that = this,
        index0 = this.data.index0,
        index1 = this.data.index1,
        index = this.data.index,
        params = {
          type: that.data.type,
          dc_id2: this.data.rules[index0].dc_id,
          dc_id1: this.data.array[index].dc_id,
          dc_id: this.data.device[index1].dc_id,
          buy_time: formData.date
        };
      wh.Post(postUrl, params, function (res) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          complete: function () {
            setTimeout(function () {
              wx.switchTab({
                url: '../index/index'
              })
            }, 2000);
          }
        });
      })
    }
  },
  firstbt: function() {
    let postUrl = '/api/madetoorder.decorate.index/get_decorate_list',
      that = this,
      device = this.data.device,
      postData = {
        type: this.data.type,
        pid: 0
      };
    wh.Post(postUrl, postData, function(res) {
      device = device.concat(res)
      that.setData({
        device: device,
      })
    });
  },
  secondbt:function(){
    let postUrl = '/api/madetoorder.decorate.index/get_decorate_list',
      that = this,
      array = this.data.array,
      index1=this.data.index1,
      pid = this.data.device[index1].dc_id;
      let postData = {
        type: this.data.type1,
        pid: pid
      };
    wh.Post(postUrl, postData, function (res) {
      array = array.concat(res)
      that.setData({
        array: array
      })
    });
  },
  threebt:function(){
    let postUrl = '/api/madetoorder.decorate.index/get_decorate_list',
      that = this,
      rules = this.data.rules,
      index=this.data.index,
      pid = this.data.array[index].dc_id;
      let postData = {
        type: this.data.type1,
        pid: pid
      };
    wh.Post(postUrl, postData, function (res) {
      rules = rules.concat(res)
      that.setData({
        rules: rules
      })
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let userInfoStr = wx.getStorageSync('W_Userinfo');
    let that = this;
    if (userInfoStr != '' && userInfoStr != undefined) {
      let userInfo = JSON.parse(wx.getStorageSync('W_Userinfo'));
      if (userInfo != '') {
        this.setData({
          'username': userInfo.nickname,
          'avatar': userInfo.avatar
        })
      }
    }
    this.firstbt();
    app.get_page_cover(this.data.type).then(function(res) {
      that.setData({
        cover_img: res
      })
    });
  },
  // 取消授权
  cancel_auth: function() {
    wx.switchTab({
      url: '../index/index'
    });
    this.setData({
      auth_modal: false
    })
  },
  /**
   * 确认授权
   */
  bindGetUserInfo: function(e) {
    let userInfo = e.detail,
      that = this;
    this.setData({
      userInfo: userInfo
    });
    if (e.detail.userInfo) {
      wx.showToast({
        title: '授权成功',
      })
      setTimeout(function() {
        let auth_rs = app.wlogin(userInfo);
        let employId = wx.getStorageSync('employId');
        if (!employId || employId == '') {
          app.employIdCallback = employId => {
            if (employId != '') {
              that.setData({
                auth_modal: false,
                username: userInfo.userInfo.nickName,
                avatar: userInfo.userInfo.avatarUrl
              })
            }
          }
        }
      }, 1500)
    } else {
      wx.switchTab({
        url: '../index/index'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    wx.getExtConfig({
      success: function(res) {
        module.exports.globalData = res.extConfig;
        let s_app_id = wx.getStorageSync('whlw_app_id');
        if (s_app_id != module.exports.globalData.app_id) {
          that.setData({
            auth_modal: true
          })
        }
      }
    });
    app.is_user_save();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})